﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace cubeworld
{
    class API
    {
        public static int login(string username, string password)
        {
            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Cubeworlds");
            Stream data = client.OpenRead("http://www.cubeworlds.de/2b4218df79f75b74462d6a49afce5c20/fe42e08d42d6a4659d87930edf416264.php?ed7b28184b07ccf81fe368bd68648d6c=" + username + "&b0e52a1510c9012ebae9e9dc1ae0c46e=" + password);
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            data.Close();
            string[] package = s.Split('|');
            if (package[0] == "true")
            {
                return Convert.ToInt32(package[2]);
            }
            else if (package[0] == "false")
            {
                if (package[1] == "Username falsch") { throw new Exception("101"); }
                if (package[1] == "Passwort falsch") { throw new Exception("102"); }
                if (package[1] == "Unbekannter Fehler") { throw new Exception("103"); }
            }
            else
            {
                throw new Exception("104");
            }
            return -1;
        }

        public static void register(string username, string password, string email)
        {
            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Cubeworlds");
            Stream data = client.OpenRead("http://www.cubeworlds.de/2b4218df79f75b74462d6a49afce5c20/1a6c08a7f2e1892b671f6fdef613e55c.php?ed7b28184b07ccf81fe368bd68648d6c=" + username + "&696d29e0940a4957748fe3fc9efd22a3=" + password + "&adc329df80ca9552f2d82126884daea7=" + email);
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            data.Close();
            string[] package = s.Split('|');
            if (package[0] == "true")
            {
                throw new Exception("001");
            }
            else if (package[0] == "false")
            {
                if (package[1] == "Unbekannter Fehler")
                {
                    throw new Exception("002");
                }
                else
                {
                    throw new Exception("003");
                }
            }
            else
            {
                throw new Exception("004");
            }
        }
    }
}
